﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfControlDemo.Models;
using WpfControlDemo.ViewModels.Base;

namespace WpfControlDemo.ViewModels.控件
{
    public class DataGridViewModel : INotifyPropertyChanged
    {
        public DataGirdModel DataGirdModel { get; set; }
        /// <summary>
        /// 删除按钮
        /// </summary>
        public CommandBase ButtonDeleteCommand { get; set; }

        public DataGridViewModel()
        {
            DataGirdModel = new DataGirdModel();
            DataGirdModel.StudentModels = GetStudentModel();
            DataGirdModel.ClassModels = GetClassModel();
            HeaderPhone = "动态标题";
            ButtonDeleteCommand= new CommandBase(DeleteSelected);
        }

        private bool isAllChecked;

        public bool IsAllChecked
        {
            get { return isAllChecked; }
            set
            {
                isAllChecked = value;
                foreach (var model in DataGirdModel.StudentModels)
                {
                    model.Checked = isAllChecked;
                }
            }
        }


        /// <summary>
        /// 此处模拟了数据库或者接口查询
        /// </summary>
        /// <returns></returns>
        public ObservableCollection<StudentModel> GetStudentModel()
        {
            ObservableCollection<StudentModel> students = new ObservableCollection<StudentModel>();
            for (int i = 1; i <= 5; i++)
            {
                StudentModel studentModel = new StudentModel();
                studentModel.StuId = 123 + i;
                studentModel.StuName = $"ZHY{i}";
                studentModel.ClassId = i;
                studentModel.Sex = "男";
                studentModel.Sex2 = 1;
                studentModel.Phone = $"1881154138{i}";

                students.Add(studentModel);
            }
            return students;
        }

        /// <summary>
        /// 此处模拟了数据库或者接口查询
        /// </summary>
        /// <returns></returns>
        public ObservableCollection<ClassModel> GetClassModel()
        {
            ObservableCollection<ClassModel> students = new ObservableCollection<ClassModel>();
            for (int i = 1; i <= 5; i++)
            {
                ClassModel studentModel = new ClassModel
                {
                    ClassId = i,
                    ClassName = $"{i}班"
                };
                students.Add(studentModel);
            }
            return students;
        }

        private StudentModel selItem = new StudentModel();

        public StudentModel SelItem
        {
            get { return selItem; }
            set { selItem = value; }
        }

        #region 属性
        public event PropertyChangedEventHandler PropertyChanged;

        private string headerPhone;

        public string HeaderPhone
        {
            get { return headerPhone; }
            set
            {
                headerPhone = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(HeaderPhone)));
            }
        }

        #endregion

        /// <summary>
        ///删除选中项
        /// </summary>
        /// <param name="obj"></param>
        private void DeleteSelected(object obj) 
        {
            DataGirdModel.StudentModels.Remove((StudentModel)obj);
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        public CommandBase ButtonModifyCommand
        {
            get => new CommandBase
            {
                DoExecute = new Action<object>(obj =>
                {
                    int stuId = int.Parse(obj.ToString());
                    //打开学生信息修改页面，---修改处理  。。。。。。
                    //MessageBox.Show(stuId.ToString());
                })
            };
        }

        /// <summary>
        /// 查看数据
        /// </summary>
        public CommandBase ButtonSeeCommand
        {
            get => new CommandBase
            {
                DoExecute = new Action<object>(obj =>
                {
                    string name = selItem.StuName;
                })
            };
        }

        /// <summary>
        /// 获取选中项数据
        /// </summary>
        public CommandBase ButtonSelectedCommand
        {
            get => new CommandBase
            {
                DoExecute = new Action<object>(obj =>
                {
                    // 所有被选中的学生   Jovan
                    var stus = DataGirdModel.StudentModels.ToList().Where(s => s.Checked).ToList();
                })
            };
        }

        /// <summary>
        /// 列表操作按钮点击事件
        /// </summary>
        public CommandBase DataGridButtonCommand
        {
            get => new CommandBase
            {
                DoExecute = new Action<object>(obj =>
                {
                    var item = obj as StudentModel;
                    string name = item.StuName;
                })
            };
        }
    }
}
